package be.bonamis.umpire.config;

import java.util.Arrays;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.cache.concurrent.ConcurrentMapCache;
import org.springframework.cache.support.SimpleCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import org.springframework.context.annotation.PropertySource;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;

@Configuration
//@Profile("local_H2_unit")
@Profile("default")
@PropertySource("classpath:h2_database.properties")
public class LocalDataSourceConfiguration implements DataSourceConfiguration {

	@Autowired
	private Environment environment;

	@Bean
	public DataSource dataSource() throws Exception {
		return new EmbeddedDatabaseBuilder()
				.setName("ump")
				.setType(EmbeddedDatabaseType.H2)
				.addScript("classpath:database/hsql/schema.sql")
				.addScript("classpath:database/hsql/test-data.sql")
				.build();
	}

	@Bean
	public CacheManager cacheManager() throws Exception {
		SimpleCacheManager scm = new SimpleCacheManager();
		Cache cache = new ConcurrentMapCache("customers");
		scm.setCaches(Arrays.asList(cache));
		return scm;
	}

	@Override
	public Map<String, String> contributeJpaEntityManagerProperties()
			throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

//	public Map<String, String> contributeJpaEntityManagerProperties() {
//		Map<String, String> p = new HashMap<String, String>();
//		p.put(org.hibernate.cfg.Environment.HBM2DDL_AUTO, "create");		
//		p.put(org.hibernate.cfg.Environment.DIALECT, H2Dialect.class.getName());
//		p.put(org.hibernate.cfg.Environment.SHOW_SQL, "true");
//		return p;
//	}
}
